package jk.spider.util.cheat.impl;

import jk.spider.core.SpiderController;
import jk.spider.util.CookieUtil;
import jk.spider.util.cheat.BaseSpiderCheatImpl;

import org.apache.log4j.Logger;

public class CookieCheat extends BaseSpiderCheatImpl {
	private static final Logger log = Logger.getLogger(CookieCheat.class);
	private volatile boolean lock = false;

	public CookieCheat(SpiderController controller) {
		super(controller);
	}

	public void execute() {
		synchronized (this) {
			if (lock)
				try {
					this.wait();
					return;
				} catch (InterruptedException e) {

				}
		}
		lock = true;
		CookieUtil cookieUtil = CookieUtil.getBrowser();
		cookieUtil.cleanCookie();
		String cookie = cookieUtil.requestCookie();
		controller.setCookie(cookie);
		
		synchronized (this) {
			lock = false;
			this.notifyAll();
		}
	}
	
}
